package cn.sh.basic.controller;

import cn.sh.basic.domain.dto.LoginDto;
import cn.sh.basic.domain.dto.WxBinderDto;
import cn.sh.basic.exception.CustomException;
import cn.sh.basic.service.ILoginService;
import cn.sh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 账号密码登录
     * @param dto
     * @return
     */

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto dto){
        try {
            return AjaxResult.me().setResultObj(loginService.accountLogin(dto));
        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("服务器繁忙");
        }
    }

    /**
     * 微信登录
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String, String>map) {
        try {
            return loginService.wechat(map);
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("服务器繁忙");
        }
    }

    /**
     * 微信登录
     * @param dto
     * @return
     */
    @PostMapping("/binder")
    public AjaxResult binder(@RequestBody WxBinderDto dto){
        try {
            return loginService.binder(dto);
        } catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("服务器繁忙");
        }
    }
}
